package com.eistert.java.datastructure._07_Set;

import com.eistert.java.datastructure._04_LinkedList._04_LinkedList;
import com.eistert.java.util.FileOperation2;

import java.util.ArrayList;

/**
 * @Description: linkedList implement the Set implementation code
 * @Author: ai
 * @create: 2023-04-05 00:37
 */
public class _07_LinkedListSet<E> implements _07_Set<E> {
    private _04_LinkedList<E> list;

    @Override
    public void add(E e) {
        if (!list.contains(e)) {
            list.addFirst(e);
        }
    }

    @Override
    public boolean contains(E e) {
        return list.contains(e);
    }

    @Override
    public void remove(E e) {
        list.removeElement(e);
    }

    @Override
    public int getSize() {
        return list.getSize();
    }

    @Override
    public boolean isEmpty() {
        return list.isEmpty();
    }

    public static void main(String[] args) {

        System.out.println("Pride and Prejudice");

        ArrayList<String> words1 = new ArrayList<>();
        if (FileOperation2.readFile("pride-and-prejudice.txt", words1)) {
            System.out.println("Total words: " + words1.size());

            _07_LinkedListSet<String> set1 = new _07_LinkedListSet<>();
            for (String word : words1)
                set1.add(word);
            System.out.println("Total different words: " + set1.getSize());
        }

        System.out.println();


        System.out.println("A Tale of Two Cities");

        ArrayList<String> words2 = new ArrayList<>();
        if (FileOperation2.readFile("a-tale-of-two-cities.txt", words2)) {
            System.out.println("Total words: " + words2.size());

            _07_LinkedListSet<String> set2 = new _07_LinkedListSet<>();
            for (String word : words2)
                set2.add(word);
            System.out.println("Total different words: " + set2.getSize());
        }
    }
}
